<?php
namespace app\models\auth_app\msg\douyin;

use app\models\AuthAccount;
use app\models\AuthComment;
use app\models\AuthItem;
use app\models\AuthUser;
use Yii;
use app\components\Func;

/**
 * webhook评论回复".
 */
class Itemcommentreply extends \app\models\AuthAppMsg
{
    public function run(){
        $msg_arr = json_decode($this->content, 1);
        $content_arr = json_decode($msg_arr['content'], 1);
//        print_r($content_arr);exit;

        $authItem = AuthItem::getObjByItemId($content_arr['reply_to_item_id']);
        if(empty($authItem))return;
//        print_r($content_arr);exit;
//        print_r(__LINE__)."/r/n";
        AuthUser::getDb()->createCommand()->upsert(AuthUser::tableName(),[
            'auth_app_id' => $this->auth_app_id,
            'auth_account_id' => $authItem->auth_account_id,
            'open_id' => $content_arr['comment_user_id'],
            'nickname' => '无',
        ])->execute();
        $authUser = AuthUser::getObjByOpenId($content_arr['comment_user_id']);
//        print_r($authUser);exit;
        AuthComment::getDb()->createCommand()->upsert(AuthComment::tableName(),[
            'auth_app_id' => $this->auth_app_id,
            'auth_account_id' => $authItem->auth_account_id,
            'auth_item_id' => $authItem->id,
            'auth_uid' => $authUser->id,
            'parent_id' => 0,
            'comment_id' => $content_arr['comment_id'],
            'content' => $content_arr['content'],
            'create_time' => $content_arr['create_time'],
            'post_time' => $content_arr['create_time'],
        ],[
            'content' => $content_arr['content'],
        ])->execute();
//        print_r(__LINE__)."/r/n";

        //推送新消息
        AuthAccount::pushNewMsg($authItem->authAccount->uid,AuthAccount::NEW_MSG_TYPE_COMMENT);
//        print_r($content_arr);exit;
        return json_encode($msg_arr['content'],JSON_UNESCAPED_UNICODE);
    }
}
